package com.zkca.xf.ui.activity.personprotect;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zkca.xf.ConstantsXF;
import com.zkca.xf.Constants_SP;
import com.zkca.xf.MyApplication;
import com.zkca.xf.R;
import com.zkca.xf.base.BaseTitleActivity;
import com.zkca.xf.model.javabean.DeviceLBean;
import com.zkca.xf.model.javabean.uploadbean.RequestInspect;
import com.zkca.xf.presenter.DeviceListPresenter;
import com.zkca.xf.ui.customview.XCRoundRectImageView;
import com.zkca.xf.utils.SPUtil;
import com.zkca.xf.utils.TimeUtils;
import com.zkca.xf.utils.ToastUtils;
import com.zkca.xf.view.Device_LView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Device_XActivity extends BaseTitleActivity implements Device_LView {

    @BindView(R.id.image1)
    XCRoundRectImageView image1;
    @BindView(R.id.image2)
    XCRoundRectImageView image2;
    @BindView(R.id.image3)
    XCRoundRectImageView image3;
    @BindView(R.id.devid)
    TextView devid;
    @BindView(R.id.devicename)
    TextView devicename;
    @BindView(R.id.devicetypename)
    TextView devicetypename;
    @BindView(R.id.danweiname)
    TextView danweiname;
    @BindView(R.id.deviceadss)
    TextView deviceadss;
    @BindView(R.id.devicestatus)
    TextView devicestatus;
    @BindView(R.id.bumenname)
    TextView bumenname;
    @BindView(R.id.careattime)
    TextView careattime;
    DeviceLBean.DataBean databean;
    @BindView(R.id.scrollView)
    ScrollView scrollView;
    @BindView(R.id.header_bar_photo_back)
    ImageView headerBarPhotoBack;
    @BindView(R.id.header_bar_photo_delete)
    ImageView headerBarPhotoDelete;
    @BindView(R.id.bigimage)
    ImageView bigimage;
    @BindView(R.id.ll_bigimage)
    LinearLayout llBigimage;

    String deviceId;
    @BindView(R.id.lastuser)
    TextView lastuser;
    @BindView(R.id.lasttime)
    TextView lasttime;
    @BindView(R.id.lastcommon)
    TextView lastcommon;
    private DeviceListPresenter deviceListPresenter;
    private RequestInspect requestInspect;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device__x);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        if (intent.hasExtra("deviceId")) {
            deviceId = intent.getStringExtra("deviceId");
            deviceListPresenter = new DeviceListPresenter(this, this);
            initDvData(deviceId);

        } else {
            databean = (DeviceLBean.DataBean) intent.getSerializableExtra("databean");
            initMyView();
        }


    }

    private void initDvData(String deviceId) {
        Map map = new HashMap();
        map.put("devId", deviceId);
        requestInspect = new RequestInspect();
        requestInspect.setParam(map);
        requestInspect.setPageNum(1);
        requestInspect.setPageSize(10);
        requestInspect.setAccessId(SPUtil.getString(this, Constants_SP.ACCESSID));
        requestInspect.setAction(2);
        requestInspect.setTimeStamp(TimeUtils.getTime());
        requestInspect.setSign(MyApplication.getInstance().generateDvSign(requestInspect));
        deviceListPresenter.getDevicelist(requestInspect);
    }

    private void initMyView() {


        setTitleText("巡点详情");
        setleftImgVisibility(true);
        final String devImage1 = databean.getDevImage1();
        String devImage2 = (String) databean.getDevImage2();
        String devImage3 = (String) databean.getDevImage3();

        String devId = databean.getDevId();             //ID
        devid.setText(devId);
        String typeName = databean.getTypeName();           //设备类型
        devicetypename.setText(typeName);
        String devName = databean.getDevName();             //设备名
        devicename.setText(devName);
        String installPosition = databean.getInstallPosition(); //地址
        deviceadss.setText(installPosition);
        int devStatus = databean.getDevStatus();                //状态
        if (devStatus == 0) {
            devicestatus.setText("安全");
            devicestatus.setTextColor(Color.rgb(52, 209, 78));
        } else {
            devicestatus.setText("故障");
            devicestatus.setTextColor(Color.rgb(216, 29, 29));
        }
        String unitName = databean.getUnitName();       //单位名称
        danweiname.setText(unitName);
        String deptName = databean.getDeptName();       //部门名称
        bumenname.setText(deptName);
        long createTime = databean.getCreateTime();    //创建时间
        careattime.setText(TimeUtils.getFormatDate2(createTime));

        lastuser.setText(databean.getLastUserName());
        long updateTime = databean.getLastTaskTime();
        lasttime.setText( TimeUtils.getFormatDate2(updateTime));
        String name1 = databean.getComment();
        if (name1==null){
            lastcommon.setText("尚未巡检");
            lastuser.setText("尚未巡检");
            lasttime.setText("尚未巡检");
        }else {
            boolean contains = name1.contains("<br/>");
            if (contains){
                name1= name1.replace("<br/>","");
            }
            lastcommon.setText(name1);
        }

        Glide.with(Device_XActivity.this).load(ConstantsXF.URL_SERVER_CE + devImage1).asBitmap().placeholder(R.mipmap.zwf).error(R.mipmap.zwf).into(image1);
        image1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                bigimage.setImageBitmap(((BitmapDrawable) image1.getDrawable()).getBitmap());
                llBigimage.setVisibility(View.VISIBLE);

                setTitleBarVisibility(false);
                scrollView.setVisibility(View.GONE);
                //
            }
        });
        if (devImage2 != null) {
            image2.setVisibility(View.VISIBLE);
            Glide.with(Device_XActivity.this).load(ConstantsXF.URL_SERVER_CE + devImage2).asBitmap().placeholder(R.mipmap.zwf).error(R.mipmap.zwf).into(image2);
            image2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    bigimage.setImageBitmap(((BitmapDrawable) image2.getDrawable()).getBitmap());
                    llBigimage.setVisibility(View.VISIBLE);

                    setTitleBarVisibility(false);
                    scrollView.setVisibility(View.GONE);
                    //
                }
            });
        }
        if (devImage3 != null) {
            image3.setVisibility(View.VISIBLE);
            Glide.with(Device_XActivity.this).load(ConstantsXF.URL_SERVER_CE + devImage3).asBitmap().placeholder(R.mipmap.zwf).error(R.mipmap.zwf).into(image3);
            image3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    bigimage.setImageBitmap(((BitmapDrawable) image3.getDrawable()).getBitmap());
                    llBigimage.setVisibility(View.VISIBLE);

                    setTitleBarVisibility(false);
                    scrollView.setVisibility(View.GONE);
                    //
                }
            });

        }


        headerBarPhotoBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                llBigimage.setVisibility(View.GONE);
                //退场动画

                scrollView.setVisibility(View.VISIBLE);
                setTitleBarVisibility(true);
            }
        });

    }

    @Override
    public void onBackPressed() {
        if (llBigimage.getVisibility() == View.VISIBLE) {
            llBigimage.setVisibility(View.GONE);
            //退场动画
            scrollView.setVisibility(View.VISIBLE);
            setTitleBarVisibility(true);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void showProgressDialog() {

    }

    @Override
    public void hideProgressDialog() {

    }

    @Override
    public void showError(String msg) {
        ToastUtils.show(mContext, "请求失败，错误原因：" + msg);
        finish();
    }

    @Override
    public void getDev(DeviceLBean deviceLBean) {
        List<DeviceLBean.DataBean> data = deviceLBean.getData();
        if (data.size() >= 1) {
            databean = data.get(0);
            initMyView();
        } else {
            ToastUtils.show(mContext, "该巡点信息尚未采集！");
            finish();
        }

    }
}
